package com.ruoyi.ucar.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ucar.boreq.CarouselImageDetailQueryBoReq;
import com.ruoyi.ucar.boreq.CarouselImageListQueryBoReq;
import com.ruoyi.ucar.boresp.*;
import com.ruoyi.ucar.domain.TCarouselImage;
import com.ruoyi.ucar.domain.po.CarouselImageDetailQueryPo;
import com.ruoyi.ucar.domain.po.CarouselImageListPo;
import com.ruoyi.ucar.domain.po.CarouselImageMobileListPo;
import com.ruoyi.ucar.mapper.TCarouselImageMapper;
import com.ruoyi.ucar.moreq.CarouselImageDetailQueryMoReq;
import com.ruoyi.ucar.moreq.CarouselImageListQueryMoReq;
import com.ruoyi.ucar.moreq.CarouselImageMobileListQueryMoReq;
import com.ruoyi.ucar.service.ISysFileMainService;
import com.ruoyi.ucar.service.ITCarouselImageService;
import com.ruoyi.ucar.util.CommonUtil;
import com.ruoyi.ucar.voreq.CarouselImageAddVoReq;
import com.ruoyi.ucar.voreq.CarouselImageUpdateVoReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 轮播图主Service业务层处理
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Service
public class TCarouselImageServiceImpl extends ServiceImpl<TCarouselImageMapper, TCarouselImage> implements ITCarouselImageService
{
    @Autowired
    private TCarouselImageMapper tCarouselImageMapper;

    @Autowired
    private ISysFileMainService iSysFileMainService;


    protected final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 查询轮播图主
     * 
     * @param id 轮播图主主键
     * @return 轮播图主
     */
    @Override
    public TCarouselImage selectTCarouselImageById(Long id)
    {
        return tCarouselImageMapper.selectTCarouselImageById(id);
    }

    @Override
    public TCarouselImage selectTCarouselImageByUuid(String carouselImageUuid) {
        return tCarouselImageMapper.selectOne(new LambdaQueryWrapper<TCarouselImage>().eq(TCarouselImage::getCarouselImageUuid, carouselImageUuid).eq(TCarouselImage::getDeleteFlag,1l));
    }

    /**
     * 查询轮播图主列表
     * 
     * @param tCarouselImage 轮播图主
     * @return 轮播图主
     */
    @Override
    public List<TCarouselImage> selectTCarouselImageList(TCarouselImage tCarouselImage)
    {
        return tCarouselImageMapper.selectTCarouselImageList(tCarouselImage);
    }


    /**
     * 批量删除轮播图主
     * 
     * @param ids 需要删除的轮播图主主键
     * @return 结果
     */
    @Override
    public int deleteTCarouselImageByIds(Long[] ids)
    {
        return tCarouselImageMapper.deleteTCarouselImageByIds(ids);
    }

    /**
     * 删除轮播图主信息
     * 
     * @param carouselImageUuid 轮播图主主键Uuid
     * @return 结果
     */
    @Override
    public int deleteTCarouselImageById(String carouselImageUuid,Long id)
    {
        TCarouselImage tCarouselImage=null;
       if(StringUtils.isNotEmpty(carouselImageUuid)) {
            tCarouselImage = this.selectTCarouselImageByUuid(carouselImageUuid);
            tCarouselImage.setDeleteFlag(-1l);
            tCarouselImage.setUpdatedBy(SecurityUtils.getUsername());
           return tCarouselImageMapper.update(tCarouselImage,new LambdaQueryWrapper<TCarouselImage>().eq(TCarouselImage::getCarouselImageUuid, carouselImageUuid));
       }else{
           tCarouselImage = this.getById(id);
           tCarouselImage.setDeleteFlag(-1l);
           tCarouselImage.setUpdatedBy(SecurityUtils.getUsername());
           return tCarouselImageMapper.update(tCarouselImage,new LambdaQueryWrapper<TCarouselImage>().eq(TCarouselImage::getId, id));
       }

    }

    @Override
    public String saveCarouselImage(CarouselImageAddVoReq carouselImageVoReq) throws IOException {
        TCarouselImage tCarouselImage=CommonUtil.objToObj(carouselImageVoReq,TCarouselImage.class);
        String sysFileMainUuid=iSysFileMainService.saveSysFile(carouselImageVoReq.getFile());
        String carouselImageUuid=CommonUtil.createUuid();
        tCarouselImage.setCarouselImageUuid(carouselImageUuid);
        tCarouselImage.setFileMainUuid(sysFileMainUuid);
        tCarouselImage.setDeleteFlag(1L);
        tCarouselImage.setCreatedBy(SecurityUtils.getUsername());
        tCarouselImageMapper.insert(tCarouselImage);
        return carouselImageUuid;
    }

    @Override
    public int updateCarouselImage(CarouselImageUpdateVoReq updateCarouselImageVoReq) throws IOException {
        TCarouselImage tCarouselImage=this.selectTCarouselImageByUuid(updateCarouselImageVoReq.getCarouselImageUuid());
        tCarouselImage.setCarouselImageTitle(updateCarouselImageVoReq.getCarouselImageTitle());
        tCarouselImage.setRemark(updateCarouselImageVoReq.getRemark());
        if(StringUtils.isNotEmpty(updateCarouselImageVoReq.getFileMainUuId())){
            tCarouselImage.setDeleteFlag(1L);
            tCarouselImage.setUpdatedBy(SecurityUtils.getUsername());
         }else {
            String sysFileMainUuid = iSysFileMainService.saveSysFile(updateCarouselImageVoReq.getFile());
            tCarouselImage.setFileMainUuid(sysFileMainUuid);
            tCarouselImage.setDeleteFlag(1L);
            tCarouselImage.setUpdatedBy(SecurityUtils.getUsername());
        }
        return tCarouselImageMapper.update(tCarouselImage,new LambdaQueryWrapper<TCarouselImage>().eq(TCarouselImage::getCarouselImageUuid,updateCarouselImageVoReq.getCarouselImageUuid()).eq(TCarouselImage::getDeleteFlag,1l));

    }

    @Override
    public int updateStatus(String carouselImageUuid, Long Status) {
        TCarouselImage tCarouselImage=this.selectTCarouselImageByUuid(carouselImageUuid);
        tCarouselImage.setStatus(Status);
        return tCarouselImageMapper.update(tCarouselImage,new LambdaQueryWrapper<TCarouselImage>().eq(TCarouselImage::getCarouselImageUuid, carouselImageUuid));
    }

    @Override
    public int batchDelete(String[] carouselImageUuids,Long[]  ids) {
        if(StringUtils.isNotNull(carouselImageUuids)&&carouselImageUuids.length>0) {
            return tCarouselImageMapper.deleteTCarouselImageByUuids(-1l, carouselImageUuids);
        }else{
            return tCarouselImageMapper.deleteTCarouselImageByids(-1l, ids);
        }
    }

    @Override
    public CarouselImageListQueryBoResp queryCarouselImageByCond(CarouselImageListQueryBoReq  carouselImageListQueryBoReq) {
        CarouselImageListQueryBoResp carouselImageListQueryBoResp=new CarouselImageListQueryBoResp();
        List<CarouselImageListQueryBo> carouselImageListQueryBos=new ArrayList<>();
        Page<CarouselImageListPo> page = new Page<>(carouselImageListQueryBoReq.getPageIndex(),carouselImageListQueryBoReq.getPageSize());
        CarouselImageListQueryMoReq carouselImageListQueryMoReq=CommonUtil.objToObj(carouselImageListQueryBoReq,CarouselImageListQueryMoReq.class);
        //删除标志
        carouselImageListQueryMoReq.setDeleteFlag(1L);
        IPage<CarouselImageListPo> carouseledImageListQueryPage = tCarouselImageMapper.carouselImageListQueryPage(page, carouselImageListQueryMoReq);
        carouselImageListQueryBoResp.setTotalCount(new Integer(String.valueOf(carouseledImageListQueryPage.getTotal())));
        carouselImageListQueryBoResp.setTotalPage(new Integer(String.valueOf(carouseledImageListQueryPage.getPages())));
        carouseledImageListQueryPage.getRecords().forEach(carouselImageListPo -> {
            CarouselImageListQueryBo carouselImageListQueryBo=CommonUtil.objToObj(carouselImageListPo,CarouselImageListQueryBo.class);
            carouselImageListQueryBo.setStatusDict(DictUtils.getDictLabel("common_status",carouselImageListPo.getStatus().toString()));
            carouselImageListQueryBos.add(carouselImageListQueryBo);
        });
        carouselImageListQueryBoResp.setCarouselImageListQueryBos(carouselImageListQueryBos);
        logger.info("轮播图查询BO端返回结果：{}"+carouselImageListQueryBoResp);
        return carouselImageListQueryBoResp;
    }

    @Override
    public CarouselImageMobileListBoResp mobileList() {
        CarouselImageMobileListBoResp carouselImageMobileListBoResp=new CarouselImageMobileListBoResp();
        CarouselImageMobileListQueryMoReq carouselImageMobileListQueryMoReq=new CarouselImageMobileListQueryMoReq();
        carouselImageMobileListQueryMoReq.setDeleteFlag(1l);
        carouselImageMobileListQueryMoReq.setStatus(1l);
        List<CarouselImageMobileListPo> carouselImageMobileListPoList= tCarouselImageMapper.carouselImageMobileListQuery(carouselImageMobileListQueryMoReq);
        List<CarouselImageMobileListBo> carouselImageMobileListBoList=new ArrayList<>();
        carouselImageMobileListPoList.forEach(carouselImageMobileListPo -> {
            CarouselImageMobileListBo carouselImageMobileListBo=CommonUtil.objToObj(carouselImageMobileListPo,CarouselImageMobileListBo.class);
            carouselImageMobileListBoList.add(carouselImageMobileListBo);
        });
        carouselImageMobileListBoResp.setCarouselImageMobileListBos(carouselImageMobileListBoList);
        return carouselImageMobileListBoResp;
    }

    @Override
    public CarouselImageDetailQueryBoResp carouselImageDetailQuery(CarouselImageDetailQueryBoReq carouselImageDetailQueryBoReq) {
        CarouselImageDetailQueryMoReq carouselImageDetailQueryMoReq=CommonUtil.objToObj(carouselImageDetailQueryBoReq,CarouselImageDetailQueryMoReq.class);
        carouselImageDetailQueryMoReq.setDeleteFlag(1l);
        List<CarouselImageDetailQueryPo> carouselImageDetailQueryPoList= tCarouselImageMapper.carouselImageDetailQuery(carouselImageDetailQueryMoReq);
        if(StringUtils.isNotNull(carouselImageDetailQueryPoList)&&carouselImageDetailQueryPoList.size()>0){
            CarouselImageDetailQueryPo carouselImageDetailQueryPo= carouselImageDetailQueryPoList.get(0);
            CarouselImageDetailQueryBoResp carouselImageDetailQueryBoResp=CommonUtil.objToObj(carouselImageDetailQueryPo,CarouselImageDetailQueryBoResp.class);
            carouselImageDetailQueryBoResp.setStatusDict(DictUtils.getDictLabel("common_status",carouselImageDetailQueryPo.getStatus().toString()));
            return carouselImageDetailQueryBoResp;
        }else{
            return null;
        }
    }
}
